사이트 내 전체검색
PHP
[ajax] 팝업창 띄우지 않고 아이디 중복확인 하기
로빈아빠
https://cmd.kr/php/582 URL이 복사되었습니다.

본문

** 1 . 따로 중복확인을 누르지 않고 아이디를 입력하고 포커스를 옮기는 순간 중복확인이 체크된다

** 2 . 중복확인 승인을 받은 후 다시 아이디를 고쳤을 경우도 체크된다

** 3 . check_id() 에들어있는 유효성체크 함수들은 여기에 표시하지 않았다 유효성 체크를 위해서는

         해당 함수들을 따로 선언해야 한다

** 4 . 모든 유효성 체크는 check_id() 에서 실시하고 submit 시킬경우 공백체크만

         go_submit() 를 통해 다시 한번 실시한다

** 5 . form 이름과 id 값 입력용 필드명만 바꾸어주면 된다 (현재 표시되어있는 mem_frm 과 salon_id

        를 해당하는 form 이름과 id 입력 필드명으로 바뿨주면 된다)

 

####### 1 . mem_join.php (회원정보 입력 페이지)

 

<script language="javascript">

var httpRequest = null;

function getXMLHttpRequest(){
     if(window.ActiveXObject){
        try{
                return new ActiveXObject("Msxml2.XMLHTTP");
        }catch(e){
                try{
                        return new ActiveXObject("Microsoft.XMLHTTP");
                }catch(e1){
                        return(null);
                }
        }
     }else if(window.XMLHttpRequest){
           return new XMLHttpRequest();
     }else{
           return null;
     }
}

function load(url){
     httpRequest = getXMLHttpRequest();
     httpRequest.onreadystatechange = viewMessage;
     httpRequest.open("GET", url, true);
     httpRequest.send(null);
}

function viewMessage(){
     if(httpRequest.readyState == 4){
           if(httpRequest.status == 200){
                  alert(httpRequest.responseText);
                  if(httpRequest.responseText != "사용 가능한 아이디입니다"){
                        var frm = document.mem_frm;
                             frm.salon_id.value = "";
                             frm.salon_id.focus();
    
                  }else{
                       var frm = document.mem_frm;
                            frm.confirm_id.value = frm.salon_id.value;
 
                  }
           }else{
                   alert("실패하였습니다 : "+httpRequest.status);
           }
     }
}

 

function check_id(url){
   var frm  = document.mem_frm;
   var id   = frm.salon_id.value;
   var c_id = frm.confirm_id.value;
   if((id.replace(/(^\s*)|(\s*$)/g,"") != "" && c_id == "x") || (c_id != "x" && c_id != id && id.replace(/(^\s*)|(\s*$)/g,"") != "")){
   
        if(chck2(id) == "f"){
             alert("아이디는 영문이나 숫자 혹은 영문과 숫자의조합만 가능합니다");
             frm.salon_id.value = "";
             frm.salon_id.focus();

             return;  
        }
        if(byteCheck(id) < 4 ){
             alert("아이디는 최소 4자리 이상 15자리 이하로 입력해주세요");
             frm.salon_id.value = "";
             frm.salon_id.focus();
             return;
        }     
        if(id.search(/\W|\s/g) > -1){
           alert("특수문자 또는 공백이 입력되었습니다\n\n특수문자와 공백은 입력할 수 없습니다.");
           frm.salon_id.value = "";
           frm.salon_id.focus();
           return;
        }
   
      var ul = url+"?id="+id;
      load(ul);    
   }
}

 

function go_submit(){

   var frm  = document.mem_frm;
   var id   = frm.salon_id.value;            

 

   if(id.replace(/(^\s*)|(\s*$)/g,"") == ""){
       alert("아이디를 입력해 주세요");
       frm.salon_id.focus();
       return;
   }

}

</script>

 

 

 

// 아이디 입력용 form

<form name="mem_frm" method="post">

   <!--한번 아이디를 승인받은 후 다시 아이디를 고쳤는지 여부를 체크하기 위한 파라미터-->

   <input type="hidden" name="confirm_id" value="x" /> 

   <input type="text" name="salon_id" size="15" maxlength="15" onblur="check_id('check_id.php')" />

   <input type="button" value="입 력" onclick="go_submit()" />

</form>

 

 

 

####### 2 . check_id.php (쿼리문 실행페이지)

<?
header("Content-Type: text/plain; charset=euc-kr");

$id = $_REQUEST['id'];     // 위의 check_id() 함수 안에서 var ul = url+"?id="+id; 선언했다

include "../connection/connect.php";
 $result = mysql_query("SELECT id FROM client_info WHERE id = '$id'");
 $num = mysql_num_rows($result);
 
 if($num < 1){
    echo("사용 가능한 아이디입니다");
 }else{
    echo("사용중인 아이디입니다 다른아이디를 입력해주세요");
 }
?>

 

 

-- check_id.php 에서 출력시킨 내용이 mem_join.php 페이지 상에서 alert 창으로 출력된다

--  반드시 mem_join.php 파일과 check_id.php 파일이 같은 폴더에 있어야 한다

-- check_id.php 에서 header("Content-Type: text/plain; charset=euc-kr"); 코드를 삽입해 주어야 alert 창에서 한글이 깨지지 않는다

댓글목록

등록된 댓글이 없습니다.

PHP
871 (8/18P)

Search

Copyright © Cmd 명령어 3.147.59.217